package com.wiz.weixin.controllor;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wiz.weixin.TO.SettingsConfigTO;
import com.wiz.weixin.common.ResponseCode;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.SettingsService;

/**
 * 通知设置
 * @author xxy
 *
 */
@Controller
@RequestMapping("/settings")
public class SettingsControllor {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	
	@Resource
	private SettingsService settingsService;
	@Resource
	private TencentManager tencentManager;
	@RequestMapping(value="/list")
	public String listSetting(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) {
		String code = request.getParameter("code");
		try {
			String openid = (String) session.getAttribute("openid");
			if(openid == null || openid.equals("")){
				openid = tencentManager.getOpenId(code);
				session.setAttribute("openid", openid);
			}
			Map<Integer,Integer>map = settingsService.listSetting(openid);
			
			model.addAttribute("map", map);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return "/setting/setting";
		
		
	}
	@RequestMapping(value="/save")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "type", required = true) int type,//站点id
			@RequestParam(value = "status", required = true) int status,//站点id
			HttpSession session, Model model) {
		try {
			String openid = (String) session.getAttribute("openid");
			
			SettingsConfigTO setting = new SettingsConfigTO(0, openid, type, status);
			settingsService.save(setting);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseCode.DATA_ERROR;
		}
		
		return ResponseCode.DATA_OK;
		
	}

}
